package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.domain.Material;
import com.ruoyi.system.domain.vo.TreeSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MaterialTypeMapper;
import com.ruoyi.system.domain.MaterialType;
import com.ruoyi.system.service.IMaterialTypeService;

/**
 * 物料类型Service业务层处理
 *
 * @author syn
 * @date 2024-01-09
 */
@Service
public class MaterialTypeServiceImpl implements IMaterialTypeService {
    @Autowired
    private MaterialTypeMapper materialTypeMapper;

    /**
     * 查询物料类型
     *
     * @param id 物料类型主键
     * @return 物料类型
     */
    @Override
    public MaterialType selectMaterialTypeById(Long id) {
        return materialTypeMapper.selectMaterialTypeById(id);
    }

    /**
     * 查询物料类型列表
     *
     * @param materialType 物料类型
     * @return 物料类型
     */
    @Override
    public List<MaterialType> selectMaterialTypeList(MaterialType materialType) {
        List<MaterialType> materialTypes = materialTypeMapper.selectMaterialTypeList(materialType);
        return materialTypes;
    }

    @Override
    public List<TreeSelect> selectMaterialTypeTreeList(MaterialType materialType){
        List<MaterialType> materialTypes = selectMaterialTypeList(materialType);
        return buildMaterialTypeTreeSelect(materialTypes);
    }

    /**
     * 新增物料类型
     *
     * @param materialType 物料类型
     * @return 结果
     */
    @Override
    public int insertMaterialType(MaterialType materialType) {
        return materialTypeMapper.insertMaterialType(materialType);
    }

    /**
     * 修改物料类型
     *
     * @param materialType 物料类型
     * @return 结果
     */
    @Override
    public int updateMaterialType(MaterialType materialType) {
        return materialTypeMapper.updateMaterialType(materialType);
    }

    /**
     * 批量删除物料类型
     *
     * @param ids 需要删除的物料类型主键
     * @return 结果
     */
    @Override
    public int deleteMaterialTypeByIds(Long[] ids) {
        return materialTypeMapper.deleteMaterialTypeByIds(ids);
    }

    /**
     * 删除物料类型信息
     *
     * @param id 物料类型主键
     * @return 结果
     */
    @Override
    public int deleteMaterialTypeById(Long id) {
        return materialTypeMapper.deleteMaterialTypeById(id);
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param materialTypes 物料类型列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildMaterialTypeTreeSelect(List<MaterialType> materialTypes) {
        List<MaterialType> materialTypeTrees = buildMaterialTypeTree(materialTypes);
        return materialTypeTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param materialTypes 物料类型列表
     * @return 树结构列表
     */
    @Override
    public List<MaterialType> buildMaterialTypeTree(List<MaterialType> materialTypes) {
        List<MaterialType> returnList = new ArrayList<>();
        List<Long> tempList = materialTypes.stream().map(MaterialType::getId).collect(Collectors.toList());
        for (MaterialType materialType : materialTypes) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(materialType.getParentId())) {
                recursionFn(materialTypes, materialType);
                returnList.add(materialType);
            }
        }
        if (returnList.isEmpty()) {
            returnList = materialTypes;
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list 物料类型集合
     * @param t    一个顶级结点
     */
    private void recursionFn(List<MaterialType> list, MaterialType t) {
        // 得到子节点列表
        List<MaterialType> childList = getChildList(list, t);
        t.setChildren(childList);

        for (MaterialType tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<MaterialType> getChildList(List<MaterialType> list, MaterialType t) {
        List<MaterialType> tlist = new ArrayList<>();
        Iterator<MaterialType> it = list.iterator();
        while (it.hasNext()) {
            MaterialType n = (MaterialType) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<MaterialType> list, MaterialType t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }


}
